package com.leetcode.LC;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class LC20 {
    public boolean isValid(String s) {

        Map<Character, Character> hashMap = new HashMap<Character, Character>();
        hashMap.put('(', ')');
        hashMap.put('[', ']');
        hashMap.put('{', '}');

        Stack<Character> stringStack = new Stack<Character>();
        for (char c : s.toCharArray()) {
            if (hashMap.containsKey(c)) {
                stringStack.push(hashMap.get(c));
            } else if (!stringStack.isEmpty() && stringStack.peek().equals(c)) {
                stringStack.pop();
            } else {
                return false;
            }
        }
        return stringStack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(new LC20().isValid("]"));
    }
}
